package com.kl.drs.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.kl.drs.bean.bo.CheckResultBO;
import com.kl.drs.bean.bo.DrsAsynchTaskBO;
import com.kl.drs.bean.bo.TaskInfoBO;
import com.kl.drs.common.BusinessKey;
import com.kl.drs.service.CheckResultService;
import com.kl.drs.service.DrsAsynchTaskService;
import com.kl.drs.service.RecordingAnalysisService;
import com.kl.drs.service.TaskInfoService;
import com.kl.drs.util.Log4jUtils;

@Component
@Configuration
@EnableScheduling
public class TaskInfoJob {
	@Autowired
	private TaskInfoService taskInfoService;
	
	@Autowired
	private RecordingAnalysisService recordingAnalysisService;
	
	@Autowired
	private CheckResultService checkResultService;
	
	@Autowired
	private DrsAsynchTaskService drsAsynchTaskService;
	private static int THREAD_MAX_COUNT = 1;
	private static int currentCount = 0;
	private static int taskCount = 10;
	@Scheduled(cron= " 0 0/5 * * * ? ")
	@Async
	public void excute(){
		Log4jUtils.logDebug("@Scheduled-------TaskInfoService.excute()---start:"+"\t\t" + new Date(System.currentTimeMillis()));
		if(currentCount<THREAD_MAX_COUNT){
			currentCount++;
			Log4jUtils.logDebug("TaskInfoService.excute()---business start:"+"\t\t" + new Date(System.currentTimeMillis()));
			boolean runFlag = true;
			while(runFlag){
			//根据异步任务信息得到业务号和类型进行上传
				try {
					Map<String, Object> condMap = new HashMap<String, Object>();
					condMap.put("taskType", BusinessKey.ASYNCH_TYPE_UB);
					condMap.put("status", BusinessKey.ASYNC_TASK_STATUS_WAIT);
					condMap.put("taskCount", taskCount);
					List<DrsAsynchTaskBO> drsAsynchTaskList=drsAsynchTaskService.getAsynchTaskList(condMap);
					if(drsAsynchTaskList!=null && drsAsynchTaskList.size()>0){
						for (DrsAsynchTaskBO drsAsynchTask : drsAsynchTaskList) {
							Map<String, Object> paramMap = new HashMap<String, Object>();
							paramMap.put("preStatus", condMap.get("status"));
							paramMap.put("businessNo", drsAsynchTask.getBusinessNo());
							paramMap.put("status",BusinessKey.ASYNC_TASK_STATUS_DEALING);
							paramMap.put("taskType", BusinessKey.ASYNCH_TYPE_UB);
							drsAsynchTaskService.updateTaskStatus(paramMap);
							TaskInfoBO taskInfo = new TaskInfoBO();
							taskInfo.setBusinessNo(drsAsynchTask.getBusinessNo());
							TaskInfoBO taskInfoBO = recordingAnalysisService.queryRecordTaskInfo(taskInfo);
							try {
								//任务信息上报以及视频上传
								taskInfoService.uploadTaskRecord(taskInfoBO);
								//对账信息以及对账异步任务生成
								CheckResultBO checkResultBO = new CheckResultBO();
								checkResultBO.setBusinessNo(taskInfoBO.getBusinessNo());
								checkResultBO.setBankCode(taskInfoBO.getBankCode());
								checkResultService.createOrUndoCheckResult(checkResultBO);
								DrsAsynchTaskBO asynchTask = new DrsAsynchTaskBO();
								asynchTask.setBusinessNo(taskInfoBO.getBusinessNo());
								asynchTask.setTaskType(BusinessKey.ASYNCH_TYPE_CR);
								asynchTask.setStatus(BusinessKey.ASYNC_TASK_STATUS_WAIT);
								drsAsynchTaskService.addAsynchTask(asynchTask);
								
								paramMap.put("preStatus", paramMap.get("status"));
								paramMap.put("status",BusinessKey.ASYNC_TASK_STATUS_SUCCESS);
							} catch (Exception ex) {
								Log4jUtils.logError("taskInfoService.excute()",drsAsynchTask.getBusinessNo()+ "资料上传保信异常："+ ex.getMessage(),ex);
								paramMap.put("preStatus", paramMap.get("status"));
								paramMap.put("status",BusinessKey.ASYNC_TASK_STATUS_FAIL);
								runFlag = false;
							}
							drsAsynchTaskService.updateTaskStatus(paramMap);
						}
					}else{
						runFlag = false;
					}
				} catch (Exception e) {
					Log4jUtils.logError("taskInfoService.excute()","@Scheduled-------taskInfoService.excute(),错误："+e.getMessage(),e);
					runFlag = false;
				}finally{
					currentCount--;
				}
			}
			Log4jUtils.logDebug("taskInfoService.excute()---business end:"+"\t\t" + new Date(System.currentTimeMillis()));
		}
		Log4jUtils.logDebug("@Scheduled-------taskInfoService.excute()---end:"+"\t\t" + new Date(System.currentTimeMillis()));
	}
	}
